import * as echarts from "echarts";
import moment from "moment/moment";
import _ from "lodash";

export const conditionList = [
  { name: "在地面At surface", EnName: null, color: "#e6194B" },
  { name: "起下钻-坐卡", EnName: null, color: "#3cb44b" },
  { name: "起下钻-空转", EnName: null, color: "#ffe119" },
  { name: "起下钻-停待", EnName: null, color: "#4363d8" },
  { name: "起下钻-中途循环", EnName: null, color: "#f58231" },
  { name: "起下钻-静止转转盘循环", EnName: null, color: "#911eb4" },
  { name: "起下钻-上提钻具", EnName: null, color: "#46f0f0" },
  { name: "起下钻-干倒划", EnName: null, color: "#f032e6" },
  { name: "起下钻-反洗", EnName: null, color: "#bcf60c" },
  { name: "起下钻-倒划", EnName: null, color: "#fabebe" },
  { name: "起下钻-下钻", EnName: null, color: "#008080" },
  { name: "起下钻-洗井", EnName: null, color: "#e6beff" },
  { name: "起下钻-干划", EnName: null, color: "#9a6324" },
  { name: "起下钻-划眼", EnName: null, color: "#fffac8" },
  { name: "钻进-坐卡", EnName: null, color: "#800000" },
  { name: "钻进-待命", EnName: null, color: "#aaffc3" },
  { name: "钻进-滑动钻进", EnName: null, color: "#808000" },
  { name: "钻进-干钻", EnName: null, color: "#ffd8b1" },
  { name: "钻进-旋转钻进", EnName: null, color: "#000075" },
  { name: "停待", EnName: null, color: "#808080" },
  { name: "钻进-静止循环", EnName: null, color: "#3cb44b" },
  { name: "钻进-停泵空转", EnName: null, color: "#000000" },
  { name: "钻进-循环空转", EnName: null, color: "#a9a9a9" },
  { name: "钻进-上提钻具", EnName: null, color: "#fabed4" },
  { name: "钻进-反洗井", EnName: null, color: "#ffd7b4" },
  { name: "钻进-干倒划", EnName: null, color: "#dcbeff" },
  { name: "钻进-倒划眼", EnName: null, color: "#000000" },
  { name: "钻进-下放钻具", EnName: null, color: "#808080" },
  { name: "钻进-洗井", EnName: null, color: "#e6194B" },
  { name: "钻进-干划眼", EnName: null, color: "#a9a9a9" },
  { name: "钻进-划眼", EnName: null, color: "#fabed4" },
  { name: "未知", EnName: null, color: "#ffd7b4" }
];
const seriesItemTemp = {
  name: "???",
  type: "custom",
  itemStyle: {
    color: "???"
  },
  renderItem: function(params, api) {
    let categoryIndex = api.value(0);
    let start = api.coord([api.value(1), categoryIndex]);
    let end = api.coord([api.value(2), categoryIndex]);
    let height = api.size([0, 1])[1] * 0.6;

    return {
      type: "rect",
      shape: echarts.graphic.clipRectByRect(
        {
          x: start[0],
          y: start[1] - height / 2,
          width: end[0] - start[0],
          height: height
        },
        {
          x: params.coordSys.x,
          y: params.coordSys.y,
          width: params.coordSys.width,
          height: params.coordSys.height
        }
      ),
      style: api.style() // 使用series.itemStyle内置样式
    };
  },
  encode: {
    x: [1, 2], // 表示维度 3、1、5 映射到 x 轴。
    y: 0 // 表示维度 2 映射到 y 轴。
  },
  data: [
    // Example: [0, start hour, end hour]
    // [1, `${today} 08:09:10`, `${today} 09:09:10`],
  ]
};

export function getNewSeriesItem(name, color) {
  let temp = {};
  // Object.assign 为浅拷贝
  temp = _.cloneDeep(seriesItemTemp);
  // Object.assign(temp, seriesItemTemp);
  temp.name = name;
  temp.itemStyle.color = color;
  // console.log(temp.itemStyle.color)
  return temp;
}

export function fakeData(count) {
  let timeArray = [];
  let todayStart = moment()
    .startOf("day")
    .valueOf(); // 使用当前日期0点时间戳
  let todayEnd = moment()
    .endOf("day")
    .valueOf();

  for (let i = 0; i < count; i++) {
    let startTime = moment(todayStart + 1000 * 60 * getRandomInt(1, 10)); // 加上随机分钟
    let endTime = moment(startTime.valueOf() + 1000 * 60 * getRandomInt(1, 15)); // 加上随机分钟

    if (endTime.isAfter(todayEnd) || startTime.isAfter(todayEnd)) {
      console.log("out off time range");
      return timeArray;
    }

    timeArray.push([
      startTime.format("YYYY-MM-DD HH:mm:ss"),
      endTime.format("YYYY-MM-DD HH:mm:ss")
    ]);
    // 设置下一个时间段的起始时间
    todayStart = endTime.valueOf();
  }
  // console.log(timeArray);
  return timeArray;
}

export function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
